// 把Date对象的值转换为YYYY-MM-DD hh:mm:ss日期类型
function formatDate(dateValue) {
    if (!dateValue) {
        return '-'
    }
    var date = new Date(dateValue)
    var month = date.getMonth() + 1
    var strDate = date.getDate()
    var seconds = date.getSeconds()
    var minutes = date.getMinutes()
    var hours = date.getHours()
    if (month < 10) {
        month = '0' + month
    }
    if (strDate < 10) {
        strDate = '0' + strDate
    }
    if (seconds < 10) {
        seconds = '0' + seconds
    }
    if (minutes < 10) {
        minutes = '0' + minutes
    }
    if (hours < 10) {
        hours = '0' + hours
    }
    var currentDate = date.getFullYear() + '-' + month + '-' + strDate + ' ' + hours + ':' + minutes + ':' + seconds
    return currentDate
}
// 批量替换数组对象key，同时过滤掉value值为undefined的
function replaceKey(arr, key) {
    let newArr = [];
    let tmp = {}
    arr.forEach((item, index) => {
        let newObj = {};
        for (var i = 0; i < key.length; i++) {
            newObj[key[i]] = item[Object.keys(item)[i]]
        }
        newArr.push(newObj);
    })

    tmp = newArr.map((item, index) => {
        for (var key in item) {
            if (item[key] === undefined) {
                delete item[key]
            }
        }
        return item
    })
    return tmp
}
// 替换查询条件汉字为相应id
function chineseToWord(chinese) {
    let arr = [{
        cond_id: 1,
        cond_str: '年份',
        cond_word: 'year'
    }, {
        cond_id: 2,
        cond_str: '学历',
        cond_word: 'xl'
    }, {
        cond_id: 3,
        cond_str: '专业',
        cond_word: 'zy'
    }, {
        cond_id: 4,
        cond_str: '地市',
        cond_word: 'addr'
    }, {
        cond_id: 5,
        cond_str: '县区',
        cond_word: 'xianqu'
    }]
    let tmp = []
    chinese.filter(item => {
        arr.map(element => {
            if (element.cond_str == item) {
                tmp.push(element.cond_word)
            }
        })
    })
    return tmp
}
// 获取用户类型，返回此用户权限列表
function roleList(userId) {
    let list = []
    switch (userId) {
        case '0':
            list = [0, 1, 2, 3, 4, 5]
            break;
        case '1':
            list = [1, 2, 5]
            break;
        case '2':
            list = [3, 4]
            break;
        default:
            list = []
            break;
    }
    return list
}

module.exports = {
    formatDate,
    replaceKey,
    chineseToWord,
    roleList
}